package gui;



import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.border.Border;

import observer.Broker;
import observer.Observer;
import javax.swing.JLabel;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Font;
import java.awt.Insets;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFormattedTextField;
import javax.swing.JButton;
import javax.swing.JOptionPane;


import utility.Auto.typeAuto;

public class StatisticheGeneraliPanel extends JPanel implements ActionListener,Observer{

	private Broker broker;

	private JLabel lblStatisticheGenerali,
	lblNumeroAuto,
	lblPercorrenzaMedia,
	lblVelocitMedia,
	lblUtilitarie,
	lblSportive,
	lblSuv,
	lblFunzioneObiettivo;

	private JFormattedTextField numTotalAutoTextField,
	kMediTextField,
	velMediaTextField,
	utilitarieTextField,
	sportTextField,
	suvTextField,
	f_oTextField;


	private JButton btnRefresh;

	private Icon getDateImage;



	public StatisticheGeneraliPanel(Broker broker) {
		// TODO Auto-generated constructor stub

		this.getDateImage = new ImageIcon("img/refreshData.jpg");
		this.broker = broker;
		broker.addObserver(this);
		this.setPreferredSize(new Dimension(250, 600));
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{19, 103, 102, 10, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 0.0, 1.0, 1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);

		lblStatisticheGenerali = new JLabel("STATISTICHE GENERALI");
		lblStatisticheGenerali.setFont(new Font("Tahoma", Font.BOLD, 13));
		GridBagConstraints gbc_lblStatisticheGenerali = new GridBagConstraints();
		gbc_lblStatisticheGenerali.insets = new Insets(0, 0, 5, 0);
		gbc_lblStatisticheGenerali.gridwidth = 4;
		gbc_lblStatisticheGenerali.gridx = 0;
		gbc_lblStatisticheGenerali.gridy = 0;
		add(lblStatisticheGenerali, gbc_lblStatisticheGenerali);

		lblNumeroAuto = new JLabel("Auto totali");
		GridBagConstraints gbc_lblNumeroAuto = new GridBagConstraints();
		gbc_lblNumeroAuto.anchor = GridBagConstraints.WEST;
		gbc_lblNumeroAuto.insets = new Insets(0, 0, 5, 5);
		gbc_lblNumeroAuto.gridx = 1;
		gbc_lblNumeroAuto.gridy = 2;
		add(lblNumeroAuto, gbc_lblNumeroAuto);

		numTotalAutoTextField = new JFormattedTextField();
		numTotalAutoTextField.setEditable(false);
		numTotalAutoTextField.setText("0");
		GridBagConstraints gbc_numTotalAutoTextField = new GridBagConstraints();
		gbc_numTotalAutoTextField.insets = new Insets(0, 0, 5, 5);
		gbc_numTotalAutoTextField.fill = GridBagConstraints.HORIZONTAL;
		gbc_numTotalAutoTextField.gridx = 2;
		gbc_numTotalAutoTextField.gridy = 2;
		add(numTotalAutoTextField, gbc_numTotalAutoTextField);

		btnRefresh = new JButton(getDateImage);
		btnRefresh.addActionListener(this);

		lblUtilitarie = new JLabel("Utilitarie");
		GridBagConstraints gbc_lblUtilitarie = new GridBagConstraints();
		gbc_lblUtilitarie.anchor = GridBagConstraints.WEST;
		gbc_lblUtilitarie.insets = new Insets(0, 0, 5, 5);
		gbc_lblUtilitarie.gridx = 1;
		gbc_lblUtilitarie.gridy = 3;
		add(lblUtilitarie, gbc_lblUtilitarie);

		utilitarieTextField = new JFormattedTextField();
		utilitarieTextField.setEditable(false);
		utilitarieTextField.setText("0 %");
		GridBagConstraints gbc_utilitarieTextField = new GridBagConstraints();
		gbc_utilitarieTextField.insets = new Insets(0, 0, 5, 5);
		gbc_utilitarieTextField.fill = GridBagConstraints.HORIZONTAL;
		gbc_utilitarieTextField.gridx = 2;
		gbc_utilitarieTextField.gridy = 3;
		add(utilitarieTextField, gbc_utilitarieTextField);

		lblSportive = new JLabel("Sportive");
		GridBagConstraints gbc_lblSportive = new GridBagConstraints();
		gbc_lblSportive.anchor = GridBagConstraints.WEST;
		gbc_lblSportive.insets = new Insets(0, 0, 5, 5);
		gbc_lblSportive.gridx = 1;
		gbc_lblSportive.gridy = 4;
		add(lblSportive, gbc_lblSportive);

		sportTextField = new JFormattedTextField();
		sportTextField.setEditable(false);
		sportTextField.setText("0 %");
		GridBagConstraints gbc_sportTextField = new GridBagConstraints();
		gbc_sportTextField.insets = new Insets(0, 0, 5, 5);
		gbc_sportTextField.fill = GridBagConstraints.HORIZONTAL;
		gbc_sportTextField.gridx = 2;
		gbc_sportTextField.gridy = 4;
		add(sportTextField, gbc_sportTextField);

		lblSuv = new JLabel("Suv");
		GridBagConstraints gbc_lblSuv = new GridBagConstraints();
		gbc_lblSuv.anchor = GridBagConstraints.WEST;
		gbc_lblSuv.insets = new Insets(0, 0, 5, 5);
		gbc_lblSuv.gridx = 1;
		gbc_lblSuv.gridy = 5;
		add(lblSuv, gbc_lblSuv);

		suvTextField = new JFormattedTextField();
		suvTextField.setEditable(false);
		suvTextField.setText("0 %");
		GridBagConstraints gbc_suvTextField = new GridBagConstraints();
		gbc_suvTextField.insets = new Insets(0, 0, 5, 5);
		gbc_suvTextField.fill = GridBagConstraints.HORIZONTAL;
		gbc_suvTextField.gridx = 2;
		gbc_suvTextField.gridy = 5;
		add(suvTextField, gbc_suvTextField);

		lblPercorrenzaMedia = new JLabel("Percorrenza media");
		GridBagConstraints gbc_lblPercorrenzaMedia = new GridBagConstraints();
		gbc_lblPercorrenzaMedia.anchor = GridBagConstraints.WEST;
		gbc_lblPercorrenzaMedia.insets = new Insets(0, 0, 5, 5);
		gbc_lblPercorrenzaMedia.gridx = 1;
		gbc_lblPercorrenzaMedia.gridy = 6;
		add(lblPercorrenzaMedia, gbc_lblPercorrenzaMedia);

		kMediTextField = new JFormattedTextField();
		kMediTextField.setEditable(false);
		kMediTextField.setText("0 Km");
		GridBagConstraints gbc_kMediTextField = new GridBagConstraints();
		gbc_kMediTextField.insets = new Insets(0, 0, 5, 5);
		gbc_kMediTextField.fill = GridBagConstraints.HORIZONTAL;
		gbc_kMediTextField.gridx = 2;
		gbc_kMediTextField.gridy = 6;
		add(kMediTextField, gbc_kMediTextField);

		lblVelocitMedia = new JLabel("Velocit\u00E0 media");
		GridBagConstraints gbc_lblVelocitMedia = new GridBagConstraints();
		gbc_lblVelocitMedia.anchor = GridBagConstraints.WEST;
		gbc_lblVelocitMedia.insets = new Insets(0, 0, 5, 5);
		gbc_lblVelocitMedia.gridx = 1;
		gbc_lblVelocitMedia.gridy = 7;
		add(lblVelocitMedia, gbc_lblVelocitMedia);

		velMediaTextField = new JFormattedTextField();
		velMediaTextField.setEditable(false);
		velMediaTextField.setText("0 Km/h");
		GridBagConstraints gbc_velMediaTextField = new GridBagConstraints();
		gbc_velMediaTextField.insets = new Insets(0, 0, 5, 5);
		gbc_velMediaTextField.fill = GridBagConstraints.HORIZONTAL;
		gbc_velMediaTextField.gridx = 2;
		gbc_velMediaTextField.gridy = 7;
		add(velMediaTextField, gbc_velMediaTextField);

		lblFunzioneObiettivo = new JLabel("Funzione obiettivo");
		GridBagConstraints gbc_lblFunzioneObiettivo = new GridBagConstraints();
		gbc_lblFunzioneObiettivo.anchor = GridBagConstraints.WEST;
		gbc_lblFunzioneObiettivo.insets = new Insets(0, 0, 5, 5);
		gbc_lblFunzioneObiettivo.gridx = 1;
		gbc_lblFunzioneObiettivo.gridy = 8;
		add(lblFunzioneObiettivo, gbc_lblFunzioneObiettivo);

		f_oTextField = new JFormattedTextField();
		f_oTextField.setEditable(false);
		GridBagConstraints gbc_f_oTextField = new GridBagConstraints();
		gbc_f_oTextField.insets = new Insets(0, 0, 5, 5);
		gbc_f_oTextField.fill = GridBagConstraints.HORIZONTAL;
		gbc_f_oTextField.gridx = 2;
		gbc_f_oTextField.gridy = 8;
		add(f_oTextField, gbc_f_oTextField);
		GridBagConstraints gbc_btnRefresh = new GridBagConstraints();
		gbc_btnRefresh.gridwidth = 2;
		gbc_btnRefresh.insets = new Insets(0, 0, 5, 5);
		gbc_btnRefresh.gridx = 1;
		gbc_btnRefresh.gridy = 11;
		add(btnRefresh, gbc_btnRefresh);

		//updateDataPanel();

	}



	public void updateDataPanel() {
		// TODO Auto-generated method stub

		int numTotAuto = broker.getAutoManaged().size();

		numTotalAutoTextField.setText(Integer.toString((numTotAuto)));
		utilitarieTextField.setText(broker.getCountAutoByType(typeAuto.CITY));
		sportTextField.setText(broker.getCountAutoByType(typeAuto.SPORT));
		suvTextField.setText(broker.getCountAutoByType(typeAuto.SUV));
		kMediTextField.setText(Double.toString(broker.getKmMedi())+" Km");

		DecimalFormat df = new DecimalFormat("#.##");

		double velMedia = broker.velocitaMediaAutoManaged(); 
		velMediaTextField.setText(df.format(velMedia)+" Km/h");

		double velIdeale = broker.velocitaMediaIdealeAutoManaged();

		double fo = (velMedia/velIdeale);
		String percentage;
		NumberFormat format;
		Locale locale = new Locale("ar");
		format = NumberFormat.getPercentInstance(locale);

		if(fo < 1){
			percentage = format.format(fo);
		}
		else
			percentage = format.format(1);

		f_oTextField.setText(percentage);

		this.repaint();

	}

	@Override
	public void update(Object object) {
		// TODO Auto-generated method stub

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub

		if(e.getSource() == btnRefresh){

			if(broker.getAutoManaged().size()>0)
				updateDataPanel();
			else
				JOptionPane.showMessageDialog(null, "Nessuna auto processata dal navigatore!\nDati non disponibili!", "Attenzione!", JOptionPane.ERROR_MESSAGE);

		}

	}

}
